package com.gw.booking.controller.backend;


import com.gw.booking.common.MessageCode;
import com.gw.booking.common.ResultObject;
import com.gw.booking.common.Util;
import com.gw.booking.dto.PageDto;
import com.gw.booking.dto.backend.MerchantDetailDtoOut;
import com.gw.booking.dto.backend.MerchantListDtoOut;
import com.gw.booking.dto.backend.MerchatDetailDto;
import com.gw.booking.dto.backend.MerchantDto;
import com.gw.booking.service.MerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@Api(tags = {"后台-商家管理"})
@Controller
@RequestMapping("/backend/merchant")
public class BMerchantController {

    @Autowired
    private MerchantService merchantService;

    @ApiOperation(value = "商家列表")
    @RequestMapping(value = "/list.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<PageDto<MerchantListDtoOut>> list(HttpServletRequest request
            , @ApiParam(value = "商家名称") @RequestParam(required = false) String shopName
            , @ApiParam(value = "营业担当") @RequestParam(required = false) String manager
            , @ApiParam(value = "商家分类") @RequestParam(required = false) Integer merchantTypeId
            , @ApiParam(value = "页数", required = true) @RequestParam int page
            , @ApiParam(value = "数量", required = true) @RequestParam int size)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, merchantService.list(shopName, manager, merchantTypeId, page, size, Util.getLoginToken(request)));
    }

    @ApiOperation(value = "商家详情")
    @RequestMapping(value = "/detial.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<MerchantDetailDtoOut> detial(HttpServletRequest request
            , @ApiParam(value = "商家id", required = true) @RequestParam Integer merchantId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, merchantService.detail(merchantId, Util.getLoginToken(request)));
    }

    @ApiOperation(value = "添加商家")
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject add(HttpServletRequest request
            , @Valid @RequestBody MerchantDto merchantDto
            , BindingResult errors)
            throws Exception {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        merchantService.txAdd(Util.getLoginToken(request), merchantDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "编辑商家")
    @RequestMapping(value = "/update.do", method = {RequestMethod.PUT})
    @ResponseBody
    public ResultObject update(HttpServletRequest request
            , @Valid @RequestBody MerchatDetailDto merchatDetailDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        merchantService.txUpdate(Util.getLoginToken(request), merchatDetailDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "删除商家")
    @RequestMapping(value = "/{merchantId}/delete.do", method = {RequestMethod.PUT})
    @ResponseBody
    public ResultObject delete(HttpServletRequest request
            , @ApiParam(value = "商家id", required = true) @PathVariable Integer merchantId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        merchantService.txDelete(Util.getLoginToken(request), merchantId);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "修改商家状态")
    @RequestMapping(value = "/{merchantId}/{state}/updateState.do", method = {RequestMethod.PUT})
    @ResponseBody
    public ResultObject updateState(HttpServletRequest request
            , @ApiParam(value = "商家id", required = true) @PathVariable Integer merchantId
            , @ApiParam(value = "状态：0非公开1公开", required = true) @PathVariable Integer state)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        merchantService.txUpdateState(Util.getLoginToken(request), merchantId, state);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "商家解除微信绑定")
    @RequestMapping(value = "/{merchantId}/unbind.do", method = {RequestMethod.PUT})
    @ResponseBody
    public ResultObject unbind(HttpServletRequest request
            , @ApiParam(value = "商家id", required = true) @PathVariable Integer merchantId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        merchantService.txUnbind(Util.getLoginToken(request), merchantId);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

//    @ApiOperation(value = "查询商家ByCode")
//    @RequestMapping(value = "/detialById.do", method = {RequestMethod.GET})
//    @ResponseBody
//    public ResultObject<Merchant> detialById(HttpServletRequest request
//            , @ApiParam(value = "商家id", required = true) @RequestParam Integer merchantId)
//            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
//        return new ResultObject(MessageCode.CODE_SUCCESS, merchantService.getMerchant(merchantId));
//    }

}
